﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Sprout.Core.Extensions;
using Sprout.Core.Utils;
using Newtonsoft.Json.Linq;
using Sprout.Api.Test.Models;

namespace Sprout.Api.Test.Controllers
{
    public class TestController : ApiControllerBase
    {

        /// <summary>
        /// 根据正则提取开始和结束字符的
        /// </summary>
        /// <param name="content"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="contain">是否包含开始符和结束符</param>
        /// <returns></returns>
        [HttpPost]
        public List<string> ExtractKeywords(string content, string begin, string end, bool contain)
        {
            var list = StrHelper.ExtractKeywords(content, begin, end);
            return list;
        }
        /// <summary>
        /// 根据属性名替换文本内容
        /// </summary>
        /// <param name="content">文本</param>
        /// <param name="begin">属性名前增加的拼接字符</param>
        /// <param name="end">属性名后增加的拼接字符</param>
        /// <returns>替换后的文本</returns>
        [HttpPost]
        public string Replace(string content, string begin, string end)
        {
            var keywords = StrHelper.ExtractKeywords(content, begin, end, false);
            Dictionary<string, string> keyValues = new Dictionary<string, string>();
            foreach (var item in keywords)
            {
                switch (item)
                {
                    case "联系人":
                        keyValues[item] = "张三";
                        break;
                    case "电话":
                        keyValues[item] = "1365846";
                        break;
                    default:
                        break;
                }
            }

            return StrHelper.Replace(content, keyValues, begin, end);
        }

        [HttpPost]
        public object SetModel()
        {
            UserInfo user1 = new UserInfo()
            {
                Id = Guid.NewGuid(),
                Name = "用户1",
                Login = new Login()
                {
                    LoginName = "admin",
                    Password = "123456"
                },
                Roles = new List<Role>() {
                    new Role(){ Id=1, Name="管理员" },
                    new Role(){ Id=2,Name="用户"}
                }
            };

            UserInfo user2 = new UserInfo();

            ReflectionHelper.SetModel(user1, user2);

            return user2;
        }
    }
}
